package ups.glre.news.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import ups.glre.news.dao.CategoryDAO;
import ups.glre.news.dao.UserDAO;
import ups.glre.news.model.Category;
import ups.glre.news.model.User;
import ups.glre.news.service.UserService;

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@Local
public class UserServiceImpl implements UserService{
	
	@Resource
	private SessionContext sessionContext;
	
	@EJB
	private UserDAO userDAO;
	
	@EJB
	private CategoryDAO categoryDAO;
	
	@PersistenceContext(unitName = "PersistenceManager")
	private EntityManager entityManager;
	
	public UserServiceImpl(){
//		createUser("Yakhya","Dabo","yakhya.dabo","12345");
//		createUser("Julien","Martin","julien.martin","12345");
//		createUser("Ludovic","Dungar","ludovic.dungar","12345");
//		createUser("Adrien","Lassere","adrien.lassere","12345");
//		userDAO = new UserDAOImpl(entityManager);
	}
	
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	@Override
	public void createUser(String firstName, String lastName, String login, String password){
		User user = new User(firstName,lastName,login, password);
		userDAO.createObject(user);
	}
	
	@Override
	public boolean authenticate(String login, String password){
		User user = null;
		try{
			user = userDAO.findUserByLoginAndPassword(login, password);			
		}	catch(Exception e){
			
		}		
		return (user!= null);
	}	
	
	@Override
	public User getUserByLogin(String login){
		User user = null;
		try{
			user = userDAO.findUserByLogin(login);			
		}	catch(Exception e){
			
		}		
		return user;
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	@Override
	public void populateUserTables() {
		
		UserService businessObject = this.sessionContext.getBusinessObject(UserService.class);
		
		if (getUserByLogin("yakhya.dabo") == null)
			businessObject.createUser("Yakhya","Dabo","yakhya.dabo","12345");
		
		if (getUserByLogin("julien.martin") == null)
			businessObject.createUser("Julien","Martin","julien.martin","12345");
		
		if (getUserByLogin("ludovic.dungar") == null)
			businessObject.createUser("Ludovic","Dungar","ludovic.dungar","12345");
		
		if (getUserByLogin("adrien.lassere") == null)
			businessObject.createUser("Adrien","Lassere","adrien.lassere","12345");
	}

	@Override
	public void subscribeUserToCategory(int userId, int categoryId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public User subscribeUserToCategory(int userId, List<Integer> categoriesId) {
User user = userDAO.findObjectById(userId);
		
		for (Integer categoryId:categoriesId){
			Category category = categoryDAO.findObjectById(categoryId);
			System.out.println("Subscribing user : " + user + " to category : " + category);
			user.subscribeToCategory(category);
			//categoryDAO.updateObject(category);
		}
		
		user = userDAO.updateObject(user);
		return user;
	}

	@Override
	public void unSubscribeUserFromCategory(int userId, int categoryId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public User unSubscribeUserFromCategory(int userId, List<Integer> categoriesId) {
		User user = userDAO.findObjectById(userId);
		
		for (Integer categoryId:categoriesId){
			Category category = categoryDAO.findObjectById(categoryId);
			System.out.println("Unsubscribing user : " + user + " from category : " + category);
			user.unSubscribeFromCategory(category);
			//categoryDAO.updateObject(category);
		}
		
		user = userDAO.updateObject(user);
		return user;
	}
	
}
